package com.freight.order.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.freight.core.common.exception.FreightException;
import com.freight.order.entity.OrderTrack;
import com.freight.order.vo.OrderTrackVo;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class JSONUtils {


    public static String dateFormat(String dateStr){

        SimpleDateFormat formatYYYY = new SimpleDateFormat("yyyyMMdd/HHmmss");
//        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        try {
//            System.out.println(formatYYYY.parse(dateStr).getTime());
//            return format1.format(formatYYYY.parse(dateStr));
            return ""+formatYYYY.parse(dateStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static float getLs(float lls){
        DecimalFormat df=new DecimalFormat("0.000000");//设置保留位数
        return Float.parseFloat(df.format(lls/600000));
    }

    /**
     * 封存轨迹信息
     */
    public static List<OrderTrack> getGJO(Long orderID, String carNum, JSONObject jsonObject) {

        System.out.println("trackInfo : "+jsonObject);
        List<OrderTrack> tracks = new ArrayList<OrderTrack>();
        JSONArray arr = JSONArray.parseArray(jsonObject.getString("records"));
        String date;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(arr!=null && arr.size()>0){
            for (int i = 0; i < arr.size(); i++) {
                String trackStr = arr.get(i).toString();
                JSONObject trackJSON = JSONObject.parseObject(trackStr);

                Double latF = Double.parseDouble(trackJSON.getString("callat"));
                Double lonF = Double.parseDouble(trackJSON.getString("callon"));
                date = sdf.format(trackJSON.getLong("arrivedtime"));
//                Map<String, Double> transform3Result = new CoordinateTransferUtils().wgs84ToGcj02(lonF, latF);
                Map<String, Double> transform3Result = new GPSCoordinateUtil().gcj_encrypt(latF,lonF);

//                float lat = Float.parseFloat(trackJSON.getString("lat")) / 600000;
//                float lon = Float.parseFloat(trackJSON.getString("lon")) / 600000;
//                String spd = trackJSON.getString("spd");
//                String mlg = trackJSON.getString("mlg");
//                String hgt = trackJSON.getString("hgt");
//                String agl = trackJSON.getString("agl");
                OrderTrack track = new OrderTrack();
                track.setAgl(trackJSON.getInteger("course").toString());
                track.setOrderId(orderID);
                track.setCarNum(carNum);
                track.setLatF(latF);
                track.setLonF(lonF);
                track.setGtm(date);
                double speed = trackJSON.getDouble("speed")/1000;
                track.setSpd(String.valueOf(speed));
                double totaldistance = trackJSON.getDouble("totaldistance")/1000;
                track.setMlg(String.valueOf(totaldistance));
                double altitude = trackJSON.getDouble("altitude")/1000;
                track.setHgt(String.valueOf(altitude));
//                track.setLatF(latF);
//                track.setLonF(lonF);
                track.setLon(transform3Result.get("lng"));
                track.setLat(transform3Result.get("lat"));
                track.setGetTime(new Date());
                track.setCarNum(carNum);
                tracks.add(track);
            }
        }


        return tracks;
    }

    /**
     * 封存历史轨迹信息
     */
    public static List<OrderTrackVo> getHistoryGJO(JSONObject jsonObject) {

        System.out.println("trackInfo : "+jsonObject);
        List<OrderTrackVo> tracks = new ArrayList<OrderTrackVo>();
        JSONArray arr = JSONArray.parseArray(jsonObject.getString("records"));
        if(arr!=null && arr.size()>0){
            for (int i = 0; i < arr.size(); i++) {
                String trackStr = arr.get(i).toString();
                JSONObject trackJSON = JSONObject.parseObject(trackStr);

                Double latF = Double.parseDouble(trackJSON.getString("callat"));
                Double lonF = Double.parseDouble(trackJSON.getString("callon"));
                Map<String, Double> transform3Result = new GPSCoordinateUtil().gcj_encrypt(latF,lonF);

                OrderTrackVo track = new OrderTrackVo(transform3Result.get("lat"), transform3Result.get("lng"),trackJSON.getDouble("speed").toString(),trackJSON.getDouble("totaldistance").toString(),trackJSON.getLong("updatetime").toString(),null);

                tracks.add(track);
            }
        }


        return tracks;
    }

    /**
     * 返回经纬度JSON
     */
    public static String getTrackJSON(List<OrderTrack> tracks) {
        String trackJSON = "[";
        for (int i = 0; i < tracks.size(); i++) {
            trackJSON += "{'x':" + tracks.get(i).getLon() + ",'y':" + tracks.get(i).getLat() + "},";
        }
        trackJSON = trackJSON.substring(0, trackJSON.length() - 1) + "]";
        return trackJSON;
    }

    /**
     * 解析高德返回的地址的经纬度
     */
    public static String getAddresL(String gdL) {
        String loacl = "";
        JSONObject object = JSONObject.parseObject(gdL);
        JSONArray geocodes = object.getJSONArray("geocodes");
        if (geocodes.size() == 1) {
            JSONObject trueAddress = geocodes.getJSONObject(0);
            String location = trueAddress.getString("location");
            String lngX = location.split(",")[0];
            String latY = location.split(",")[1];
//            System.out.println(lngX + " : " + latY);
            loacl = "{'x':" + lngX + ",'y':" + latY + "}";
        }
        return loacl;
    }
}
